package com.example.lianjuan.Base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.lianjuan.R;

public abstract class BaseFragment extends Fragment {

    protected FrameLayout mBaseContainer;
    private View mEmptyView;
    private State currentState = State.NONE;
    private View mErrorView;
    private View mLoadingView;
    private View mSuccess;
    protected LinearLayout networkClick;

    public enum State {
        /**
         * 数据为空
         */
        EMPTY,
        /**
         * 网络请求错误
         */
        ERROR,
        /**
         * 加载中
         */
        LOADING,
        /**
         * 正常状态
         */
        NONE,
        /**
         * 成功状态
         */
        SUCCESS
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = loadRootView(inflater, container);
        mBaseContainer = rootView.findViewById(R.id.base_container);
        loadStatesView(inflater, container);
        initView(rootView);
        initPresenter();
        onNetworkClick();
        initListener();
        loadData();
        return rootView;
    }

    protected void initListener() {

    }


    protected View loadRootView(LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.base_fragment_layout, container, false);
    }

    /**
     * 加载各种状态View
     *
     * @param inflater
     * @param container
     */
    protected void loadStatesView(LayoutInflater inflater, ViewGroup container) {
        mSuccess = loadSuccessView(inflater, container);
        mBaseContainer.addView(mSuccess);
        //加载空的View
        mEmptyView = loadEmptyView(inflater, container);
        mBaseContainer.addView(mEmptyView);
        //加载错误的View
        mErrorView = loadErrorView(inflater, container);
        mBaseContainer.addView(mErrorView);
        //加载的View
        mLoadingView = loadLoadingView(inflater, container);
        mBaseContainer.addView(mLoadingView);
        setUpState(State.NONE);
    }

    protected void setUpState(State state) {
        currentState = state;
        mEmptyView.setVisibility(currentState == State.EMPTY ? View.VISIBLE : View.GONE);
        mErrorView.setVisibility(currentState == State.ERROR ? View.VISIBLE : View.GONE);
        mLoadingView.setVisibility(currentState == State.LOADING ? View.VISIBLE : View.GONE);
        mSuccess.setVisibility(currentState == State.SUCCESS ? View.VISIBLE : View.GONE);
    }

    /**
     * 加载错误的View
     *
     * @param inflater
     * @param container
     */
    protected View loadErrorView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_error, container, false);
    }

    /**
     * 加载的View
     *
     * @param inflater
     * @param container
     */
    protected View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading, container, false);
    }

    /**
     * 加载空的View
     *
     * @param inflater
     * @param container
     */
    protected View loadEmptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty, container, false);
    }

    /**
     * 网络错误界面，点击按钮
     */
    protected void onNetworkClick() {
        if (mErrorView != null) {
            networkClick = mErrorView.findViewById(R.id.network_click);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        release();
    }

    protected void release() {
        //释放资源
    }

    protected void initPresenter() {
        //加载Presenter
    }

    protected void loadData() {
        //加载数据
    }

    protected void initView(View view) {
        //加载视图
    }

    protected View loadSuccessView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(getRootViewResID(), container, false);
    }

    protected abstract int getRootViewResID();
}
